﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE;
using Crude.Core;
using Crude.VisualStudioAddin.Forms;

namespace Crude.VisualStudioAddin.Actions
{
    public class CrudeProjectAction : BaseProjectAction
    {
        public override string Name
        {
            get { return "CrudeProject"; }
        }

        public override string Caption
        {
            get { return "Crude Project"; }
        }

        public override string Tooltip
        {
            get
            {
                return "Edit the csproj and set the project as crude project"; ;
            }
        }

        public override BaseProjectAction.ProjectCrudeStatus CrudeStatus
        {
            get { return ProjectCrudeStatus.None; }
        }

        protected override void InternalExecuteAction(EnvDTE80.DTE2 applicationObject, EnvDTE.Project project)
        {
            if (applicationObject.SourceControl.IsItemUnderSCC(project.FullName) &&
                !applicationObject.SourceControl.IsItemCheckedOut(project.FullName))
            {
                applicationObject.SourceControl.CheckOutItem(project.FullName);
            }

            Core.Project crudeProject = new Core.Project(project.FullName);

            if (!crudeProject.IsImportLineExisted())
            {
                UpdatePOMSettingsForm form = new UpdatePOMSettingsForm();
                form.ShowDialog();

                UpdateProjectObjectModel updateProjectObjectModel = new UpdateProjectObjectModel();
                updateProjectObjectModel.Update(project.FullName, form.OnlyCrudeDependencies,
                    form.ResolveToRemoteDependencyVersion);           

                project.ProjectItems.AddFromFile(string.Format("{0}\\{1}.pom.xml",
                    crudeProject.ProjectDirectory, crudeProject.AssemblyName));

                project.Save();

                crudeProject = new Core.Project(project.FullName);
                crudeProject.AddImportLine();
                crudeProject.Save();
            }
        }

    }
}
